package com.shangyitong.yygh_user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangyitong.Result;
import com.shangyitong.client.DictFeignClient;
import com.shangyitong.utils.AuthContextHolder;
import com.shangyitong.yygh.model.user.Patient;
import com.shangyitong.yygh_user.service.PatientService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.BlockSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
@Slf4j
public class PatientController {

    @Autowired
    private PatientService patientService;

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 查看就诊人列表
     * finished
     * @param request
     * @return
     */
    @ApiOperation(value = "查看就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        List<Patient> patients = patientService.list(new QueryWrapper<Patient>()
                .eq("user_id", AuthContextHolder.getUserId(request)));
        patients.forEach(item -> {
            item.getParam().put("certificatesTypeString", dictFeignClient.getSingleName(item.getCertificatesType()));
        });
        return Result.ok(patients);
    }

    /**
     * 添加就诊人
     * finished
     * @param patient
     * @param request
     * @return
     */
    @ApiOperation(value = "添加就诊人")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        patientService.savePatient(patient, AuthContextHolder.getUserId(request));
        return Result.ok();
    }

    /**
     * 根据id查询就诊人信息
     * finished
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getById(id);
        String provinceName = dictFeignClient.getSingleName(patient.getProvinceCode());
        String cityName = dictFeignClient.getSingleName(patient.getCityCode());
        String districtName = dictFeignClient.getSingleName(patient.getDistrictCode());
        String certificatesTypeName = dictFeignClient.getSingleName(patient.getCertificatesType());
        patient.getParam().put("provinceString", provinceName);
        patient.getParam().put("cityString", cityName);
        patient.getParam().put("districtString", districtName);
        patient.getParam().put("certificatesTypeString", certificatesTypeName);
        return Result.ok(patient);
    }

    /**
     * 删除就诊人信息
     * finished
     * @param id
     * @return
     */
    @ApiOperation(value = "删除就诊人信息")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {
        boolean remove = patientService.removeById(id);
        if (remove) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改就诊人信息
     * finished
     * @param patient
     * @return
     */
    @ApiOperation(value = "修改就诊人信息")
    @PostMapping("/auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        boolean update = patientService.updateById(patient);
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取就诊人
     * @param id
     * @return
     */
    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id){
        return patientService.getById(id);
    }
}
